package xuhaijia.lanou3g.autohome.activity;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

import org.json.JSONException;
import org.json.JSONObject;

import cn.bmob.push.PushConstants;
import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.find.groupbuy.GroupBuyActivity;

/**
 * Created by 7 on 16/9/2.
 */
public class MyPushMessageReceiver extends BroadcastReceiver{

    private NotificationManager notificationManager;
    private NotificationManagerCompat notificationManagerCompat;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(PushConstants.ACTION_MESSAGE)){
            String jsonStr = intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
            String content = null;
            try {
                // 处理JSON
                JSONObject jsonObject = new JSONObject(jsonStr);
                content = jsonObject.getString("alert");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManagerCompat = NotificationManagerCompat.from(context);
            Notification notify = createNotify(context , content);
            notificationManagerCompat.notify(7, notify);
        }
    }

    private Notification createNotify(Context context , String content) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        // builder 可用链式 写法 因为返回值都是this;
        builder.setSmallIcon(R.mipmap.ahlib_about_icon_logo);

        // 设置来通知的时候 声音/ 震动 /三色灯的显示
        builder.setDefaults(Notification.DEFAULT_ALL);

        // 设置点击Notification之后, 通知消失
        builder.setAutoCancel(true);

        builder.setContentTitle("汽车之家");
        builder.setContentText(content);
        // 为Notification设置点击事件
//        PendingIntent intent = getPendingIntent();
//        builder.setContentIntent(intent);
        //  为通知设置自定义布局
//        views.setImageViewResource();
        return builder.build();

    }
}
